package com.dcinside.gallery.programming.janggi.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class BoardComposite extends Composite
{
    private Canvas top;
    private Canvas left;
    private Canvas center;
    private Canvas right;
    private Canvas bottom;
    private static Image topImage;
    private static Image leftImage;
    private static Image centerImage;
    private static Image rightImage;
    private static Image bottomImage;

    private final static int borderX = 10;
    private final static int borderY = 10;
    private final static int gridHeight = 45;
    private final static int gridWidth = 50;
    private final static int gridHeight_2 = gridHeight / 2;
    private final static int gridWidth_2 = gridWidth / 2;

    public BoardComposite( Composite parent, int style )
    {
        super( parent, style );
        if ( topImage == null )
        {
            try
            {
                loadImages();
            }
            catch ( FileNotFoundException e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        createControl();
        addBorderAreaPaintListeners();
        addCenterAreaPaintListener();
    }

    private void addCenterAreaPaintListener()
    {
        center.addPaintListener( new PaintListener()
        {
            public void paintControl( PaintEvent e )
            {
                GC gc = e.gc;
                gc.drawImage( centerImage, 0, 0 );
                drawGrid( gc );
            }
        } );

    }

    protected void drawGrid( GC gc )
    {
        gc.setForeground( getDisplay().getSystemColor( SWT.COLOR_BLACK ) );
        int i, j;
        for ( i = 0; i < 8; i++ )
            for ( j = 0; j < 9; j++ )
                gc.drawRectangle( gridWidth_2 + gridWidth * i, gridHeight_2
                    + gridHeight * j, gridWidth, gridHeight );

        gc.drawRectangle( gridWidth_2 - 1, gridHeight_2 - 1, gridWidth * 8 + 2,
            gridHeight * 9 + 2 );

    }

    private void addBorderAreaPaintListeners()
    {
        top.addPaintListener( new PaintListener()
        {

            public void paintControl( PaintEvent e )
            {
                GC gc = e.gc;
                gc.drawImage( topImage, 0, 0 );
            }
        } );
        left.addPaintListener( new PaintListener()
        {

            public void paintControl( PaintEvent e )
            {
                GC gc = e.gc;
                gc.drawImage( leftImage, 0, 0 );
            }
        } );
        right.addPaintListener( new PaintListener()
        {

            public void paintControl( PaintEvent e )
            {
                GC gc = e.gc;
                gc.drawImage( rightImage, 0, 0 );
            }
        } );
        bottom.addPaintListener( new PaintListener()
        {

            public void paintControl( PaintEvent e )
            {
                GC gc = e.gc;
                gc.drawImage( bottomImage, 0, 0 );
            }
        } );

    }

    private void createControl()
    {
        top = new Canvas( this, SWT.NONE );
        left = new Canvas( this, SWT.NONE );
        center = new Canvas( this, SWT.NONE );
        right = new Canvas( this, SWT.NONE );
        bottom = new Canvas( this, SWT.NONE );

        top.setBounds( 0, 0, 470, borderY );
        center.setBounds( borderX, borderY, 450, 450 );
        left.setBounds( 0, borderY, borderX, 460 );
        right.setBounds( 460, borderY, borderX, 460 );
        bottom.setBounds( borderX, 460, 460, borderY );
    }

    private void loadImages() throws FileNotFoundException
    {
        Device device = getDisplay();
        String imgRoot = "resources" + File.separator + "img" + File.separator;
        File topImageFile = new File( imgRoot + "board_01.gif" );
        File leftImageFile = new File( imgRoot + "board_02.gif" );
        File centerImageFile = new File( imgRoot + "board_03.gif" );
        File rightImageFile = new File( imgRoot + "board_04.gif" );
        File bottomImageFile = new File( imgRoot + "board_05.gif" );
        System.out.println( centerImageFile.getAbsolutePath() );
        topImage = new Image( device, new FileInputStream( topImageFile ) );
        leftImage = new Image( device, new FileInputStream( leftImageFile ) );
        centerImage = new Image( device, new FileInputStream( centerImageFile ) );
        rightImage = new Image( device, new FileInputStream( rightImageFile ) );
        bottomImage = new Image( device, new FileInputStream( bottomImageFile ) );
    }

}
